/*
 * @Descripttion: 
 * @version: 
 * @Author: yuanliutech
 * @Date: 2022-11-02 14:26:03
 * @LastEditors: yuanliutech@lwj
 * @LastEditTime: 2023-02-20 10:54:47
 */
const app = getApp()
import Dialog from '@vant/weapp/dialog/dialog';
import {
    showMsg
} from '../../../utils/config.js'
import {
    reqGetAdmin,
    reqOrgUserAdd
} from '../../../request/api'
Page({
    data: {
        navHeight: app.globalData.navHeight + 'px', //头部距离
        bottomHeight: app.globalData.bottomHeight + 'px', //iphone底部距离
        info: {},
        srcUsername: '../../../statics/images/personal-center/default_avatar.png',
        active: false,
        searchType: 'username',
        searchValue: '',
        user: {},
        disabledBtn: false,
        // 审批开始
        showStatus: false,
        statusApprove: 2,
        statusApproveText: "消防安全专员",
        statusList: [ {
            id: 2,
            text: '消防安全专员',
        }, {
            id: 3,
            text: '消防安全负责人',
        }, {
            id: 4,
            text: '消防监管人',
        }, {
            id: 5,
            text: '专职消防安全管理人',
        }, {
            id: 6,
            text: '楼(建筑)长',
        }, {
            id: 7,
            text: '维保人员',
        }, ],
        dataList1: [],
        page1: 1,
        isNextPage1: true,
        triggered1: false,
        showMoreActions1: false,
        listItemInfo1: {},
        // 审批结束
    },
    onLoad() {},
    onShow() {

    },
    // 点击切换用户名/手机号
    toggleSearchClick() {
        this.setData({
            active: !this.data.active
        })
    },
    // 切换搜索条件
    itemClick(e) {
        let {
            type
        } = e.currentTarget.dataset
        this.setData({
            searchType: type,
            active: !this.data.active
        })
    },
    // 获取输入值
    searchValueChange(e) {
        this.setData({
            searchValue: e.detail.value
        })
    },
    // 清除输入值
    clearUsername() {
        this.setData({
            searchValue: ''
        })
    },
    // 点击搜索成员
    async searchClick() {
        let {
            searchType,
            searchValue
        } = this.data
        if (!searchValue) return showMsg("请先输入搜索信息")
        let params = {
            username: '',
            mobile: ''
        }
        if (searchType === 'username') {
            params.username = searchValue
        } else {
            params.mobile = searchValue
        }
        const res = await reqGetAdmin(params)
        if (res.code !== 20000) return showMsg(res.message)
        this.setData({
            user: res.data.rows
        })
    },
    // 输入框搜索
    searchChange(e) {
        this.setData({
            searchValue: e.detail,
        })
        if (e.detail.length < 11) return
        clearTimeout(this.data.timer)
        this.data.timer = setTimeout(() => {
            this.searchClick1()
        }, 500)
    },
    async searchClick1() {
        let {
            searchValue
        } = this.data
        const res = await reqGetAdmin({
            mobile: searchValue
        })
        if (res.code !== 20000) return showMsg(res.message)
        this.setData({
            user: res.data.rows
        })
    },
    // 删除
    delClick() {
        this.setData({
            user: {}
        })
    },
    // 邀请点击
    async addClick() {
        if (this.data.disabledBtn) return
        if (!this.data.user.username) return showMsg("请先搜索需要邀请的用户")
        let params = {
            orgId: app.globalData.orgId ==1?'':app.globalData.orgId,
            tenantId: app.globalData.tenantId,
            userId: this.data.user.id,
            role:this.data.statusApprove
        }
        const res = await reqOrgUserAdd(params)
        if (res.code !== 20000) return showMsg(res.message)
        showMsg("已邀请")
        this.setData({
            disabledBtn: true
        })
        setTimeout(() => {
            wx.navigateBack({
                delta: 1,
            })
        }, 1000)
    },
    // 全局隐藏
    hideSearchClick() {
        this.setData({
            active: false
        })
    },
        //----------- 审批列表开始
    // 弹出审批状态
    Clickstatus() {
        this.setData({
            showStatus: true
        })
    },
     // 隐藏审批状态
     statusClose() {
        this.setData({
            showStatus: false
        })
    },
     // 确定审批状态
     statusConfirm(e) {
        const {
            value,
        } = e.detail;
        this.setData({
            "statusApprove": value.id,
            "statusApproveText": value.text,
            showStatus: false,
        })
    },
})